var dateTools = {
  convertToDate: function(timestamp) {
    var date = new Date();
    date.setTime(timestamp);
    var currentDate = new Date();
    var currentTimestamp = currentDate.getTime();
    var dateArray = {
      year: date.getFullYear(),
      month: appendzero(date.getMonth() + 1),
      day: appendzero(date.getDate()),
      hour: appendzero(date.getHours()),
      minite: appendzero(date.getMinutes()),
      second: appendzero(date.getSeconds())
    };
    var currentDateArray = {
      year: currentDate.getFullYear(),
      month: appendzero(currentDate.getMonth() + 1),
      day: appendzero(currentDate.getDate()),
      hour: appendzero(currentDate.getHours()),
      minite: appendzero(currentDate.getMinutes()),
      second: appendzero(currentDate.getSeconds())
    };
    if (dateArray.year != currentDateArray.year || dateArray.month != currentDateArray.month) {
      return dateArray.year + "/" + dateArray.month + "/" + dateArray.day;
    } else {
      if (dateArray.day == currentDateArray.day) {
        //这个地方表示是同一天
        var time = currentTimestamp - timestamp;
        if (time < 60000) {
          //小于一分钟
          return "刚刚";
        } else if (time < 3600000) {
          //小于一小时
          return parseInt(time / 60000) + "分钟前";
        } else if (time >= 3600000 && time < 86400000) {
          //小于一天
          return parseInt(time / 3600000) + "小时前";
        }
      } else if (currentDateArray.day - 1 > dateArray.day) {
        //昨天以前
        return dateArray.year + "/" + dateArray.month + "/" + dateArray.day;
      } else {
        //昨天
        return "昨天 " + dateArray.hour + ":" + dateArray.minite;
      }
    }
  },
  // 精确到时分秒
  timestampToData: function(timestamp) {
    var datetime = new Date();
    datetime.setTime(timestamp);
    var year = datetime.getFullYear();
    var month = appendzero(datetime.getMonth() + 1);
    var date = appendzero(datetime.getDate());
    var hour = appendzero(datetime.getHours());
    var minute = appendzero(datetime.getMinutes());
    var second = appendzero(datetime.getSeconds());
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  },
  // 只精确到日期
  timestampToDataDay: function(timestamp) {
    var timestamps = timestamp * 1000;
    var datetime = new Date();
    datetime.setTime(timestamps);
    var year = datetime.getFullYear();
    var month = appendzero(datetime.getMonth() + 1);
    var date = appendzero(datetime.getDate());
    return year + "-" + month + "-" + date;
  },
  // 时间戳转日期
  timestampToDataDayTwo: function(timestamp) {
    var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = appendzero(date.getDate()) + ' ';
    var h = appendzero(date.getHours()) + ':';
    var m = appendzero(date.getMinutes()) + ':';
    var s = appendzero(date.getSeconds());
    return Y + M + D + h + m + s;
  },
  // 时间戳转日期
  wanNumZh: function(num) {
    num = ''+num;
    if (num.length > 5) {
      num = num / 10000;
      num = num.toFixed(2) + "万";
      return num
    }
  },
};

function appendzero(obj) {
  if (obj < 10) return "0" + "" + obj;
  else return obj;
}
module.exports = {
  dateTools: dateTools
}